package com.lhkj.ct.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.enums.OperatorType;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.utils.ShiroUtil;
import com.lhkj.ct.meta.modules.scheduling.common.enums.AppointStatus;
import com.lhkj.ct.meta.modules.scheduling.model.dto.ScheduleSearch;
import com.lhkj.ct.meta.modules.scheduling.model.entity.TblScheAppoint;
import com.lhkj.ct.meta.modules.scheduling.model.entity.TblScheduling;
import com.lhkj.ct.meta.modules.scheduling.model.vo.SchedulingDateVo;
import com.lhkj.ct.meta.modules.scheduling.model.vo.SchedulingDoctorVo;
import com.lhkj.ct.meta.modules.scheduling.service.ScheAppointService;
import com.lhkj.ct.meta.modules.scheduling.service.SchedulingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("api/my/appoint/record")
@Api(tags = {"4.预约记录"})
public class MyAppointController{

    @Resource
    private ScheAppointService appointService;

    @Resource
    private SchedulingService schedulingService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "分页查询")
    @GetMapping("list")
    public ActionResult<IPage<TblScheAppoint>> list(TblScheAppoint appoint, PageQuery pageQuery) {
        appoint.setPatId(ShiroUtil.getAuthUserInfo().getUserId());
        return ActionResult.success(appointService.selectAppointPageByPatient(appoint, pageQuery));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "预约-排班日期列表")
    @GetMapping("scheDates")
    public ActionResult<List<SchedulingDateVo>> selectScheDates(ScheduleSearch search) {
        return ActionResult.success(schedulingService.selectScheDates(search));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "预约-排班医生列表")
    @GetMapping("scheDocs")
    public ActionResult<List<SchedulingDoctorVo>> selectScheDoctor(ScheduleSearch search) {
        return ActionResult.success(schedulingService.selectScheDoctor(search));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "预约-排班时间列表")
    @GetMapping("scheTimes")
    public ActionResult<List<TblScheduling>> selectScheTimes(ScheduleSearch search) {
        return ActionResult.success(schedulingService.selectScheTimes(search));
    }

    @PostMapping("insert")
    @ApiOperation(value = "新增预约")
    @Log(title = "新增预约", actionType = ActionType.INSERT, operatorType = OperatorType.USER)
    public ActionResult<Void> appoint(@Validated @RequestBody TblScheAppoint entity) {
        entity.setPatId(ShiroUtil.getAuthUserInfo().getUserId());
        entity.setStatus(AppointStatus.UNSIGNED);
        appointService.insert(entity);
        return ActionResult.success("预约成功");
    }

    @PutMapping("{id}/sign")
    @ApiOperation(value = "签到")
    @Log(title = "预约签到", actionType = ActionType.UPDATE, operatorType = OperatorType.USER)
    public ActionResult<Void> sign(@PathVariable String id) {
        return ActionResult.judge( appointService.userSign(id));
    }

    @DeleteMapping("{id}/cancel")
    @ApiOperation(value = "取消")
    @Log(title = "取消预约", actionType = ActionType.DELETE, operatorType = OperatorType.USER)
    public ActionResult<Void> cancel(@PathVariable String id) {
        return ActionResult.judge(appointService.userCancel(id));
    }
}
